package com.hongdee.scm.historycar.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.common.service.impl.Common;
import com.hongdee.scm.historycar.dao.ITScmHistoryCarDao;
import com.hongdee.scm.historycar.service.ITScmHistoryCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmHistoryCarServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.historycar.service.impl
 * 作  者: 术洪超
 * 日  期:2017-06-19 15:12
 */
@Service
public class TScmHistoryCarServiceImpl implements ITScmHistoryCarService {

    String tabName = "t_scm_car";
    @Autowired
    private ITScmHistoryCarDao itScmHistoryCarDao;


    /**
     * @param uid   登录用户 ID
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  参数数组
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @Override
    public Map page(String start, String limit, String uid, Map data) throws Exception {

        List params = new ArrayList();
        Common com = new Common();
        Boolean hasWhere = false;
        StringBuffer sqlbuf = new StringBuffer();
        sqlbuf.append("select tss.*,date_format(p.purchase_date,'%Y-%m-%d') purchase_date1,p.car_model model_name,p.configure configure_name from  T_SCM_STOCK tss" +
                " left join t_scm_car p on p.id=tss.car_id ");

        String oid = TokenUtil.getCompanyId(uid);
        if(!oid.equals("")&&oid!=null){
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append("tss.company_id = ? ");
            params.add(oid);
        }
        //vin条件模糊
        if (data.get("vin") != null && !StringUtils.isEmpty(data.get("vin"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" tss.vin like ? ");
            params.add("%" + data.get("vin") + "%");
        }
/*        //入库单号模糊
        if (data.get("enterWarehouseNum") != null && !StringUtils.isEmpty(data.get("enterWarehouseNum"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" p.enter_warehouse_num like ? ");
            params.add("%" + data.get("enterWarehouseNum") + "%");
        }*/
        //仓库
        if (data.get("warehouse") != null && !StringUtils.isEmpty(data.get("warehouse"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" tss.warehouse_id = ? ");
            params.add(data.get("warehouse"));
        }
        //提单号模糊
        if (data.get("orderNum") != null && !StringUtils.isEmpty(data.get("orderNum"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" p.old_num like ? ");
            params.add("%" + data.get("orderNum") + "%");
        }
        //品牌
        if (data.get("brand") != null && !StringUtils.isEmpty(data.get("brand"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" p.brand = ? ");
            params.add(data.get("brand"));
        }
        //车系
        if (data.get("carSeriesCode") != null && !StringUtils.isEmpty(data.get("carSeriesCode"))) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" p.car_series_id = ? ");
            params.add(data.get("carSeriesCode"));
            //车型代码
            if (data.get("carModelCode") != null && !StringUtils.isEmpty(data.get("carModelCode"))) {
                hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
                sqlbuf.append(" p.car_model_id = ? ");
                params.add(data.get("carModelCode"));
                //车辆配置
                if (data.get("carConfigureCode") != null && !StringUtils.isEmpty(data.get("carConfigureCode"))) {
                    hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
                    sqlbuf.append(" p.configure_id = ?");
                    params.add(data.get("carConfigureCode"));

                }
            }
        }
        //采购日期起
        if (data.get("enterWarehouseDateStart") != null && !data.get("enterWarehouseDateStart").equals("")) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" SUBSTR(p.purchase_date,1,10) >= ? ");
            params.add(data.get("enterWarehouseDateStart"));
        }
        //采购日期到
        if (data.get("enterWarehouseDateStop") != null && !data.get("enterWarehouseDateStop").equals("")) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append("SUBSTR(p.purchase_date,1,10) <= ? ");
            params.add(data.get("enterWarehouseDateStop"));
        }
        //销售状态
        if (data.get("saleFlag") != null && !data.get("saleFlag").equals("")) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            if(data.get("saleFlag").equals("0")){//未销售
                sqlbuf.append(" p.status = '1' ");
            }else{//已销售
                sqlbuf.append(" p.status = '2' ");
            }
            //params.add( data.get("saleFlag"));
        }
        //1采购入库2已售3调拨在途
        if (data.get("status") != null && !data.get("status").equals("")) {
            hasWhere = com.appendWhereIfNeed(sqlbuf, hasWhere);
            sqlbuf.append(" tss.enter_type = ? ");
            params.add(data.get("status") );
        }
        sqlbuf.append(" order by purchase_date");
        return this.itScmHistoryCarDao.page(sqlbuf.toString(), params.toArray(), start, limit);
    }
}
